package com.zzxx.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzxx.mapper.MemberMapper;
import com.zzxx.mapper.OrderMapper;
import com.zzxx.mapper.ProductMapper;
import com.zzxx.mapper.TravellerMapper;
import com.zzxx.pojo.Orders;
import com.zzxx.pojo.Traveller;
import com.zzxx.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private TravellerMapper travellerMapper;

	@Autowired
	private ProductMapper productMapper;

	@Autowired
	private MemberMapper memberMapper;

	@Override
	public PageInfo<Orders> findAll(Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		List<Orders> list = orderMapper.findAll();
		PageInfo<Orders> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public Orders findById(String id) {
		return orderMapper.findById(id);
	}

	@Override
	public boolean update(Orders order) {
		//orders
		order.setPeopleCount(order.getTravellers().size());
		orderMapper.update(order);
		//traveller
		travellerMapper.setForeign_Key_Check(0);
		travellerMapper.deleteByOid(order.getId());
		travellerMapper.setForeign_Key_Check(1);
		order.getTravellers().forEach(n->{
			Traveller sqlTraveller = travellerMapper.findTraveller(n);
			if (sqlTraveller!=null) n = sqlTraveller;
			travellerMapper.insertOrUpdate(n);
			travellerMapper.addOrderTraveller(order.getId(),n.getId());
		});
		//product
		productMapper.update(order.getProduct());
		//member
		memberMapper.update(order.getMember());
		return true;
	}
}
